package com.shangbang.verb.service.impl.record;

import com.shangbang.verb.dao.record.UserRecordMapper;
import com.shangbang.verb.entity.record.UserRecordEntity;
import com.shangbang.verb.service.record.UserRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserRecordServiceImpl implements UserRecordService {
    @Autowired
        private UserRecordMapper userRecordMapper;
   @Override
    public Object createUserRecordByType(String phone, String nickname, Integer gender, String aboutMe, String wxuid, String icon, Integer type) {
        //type:0 手机号注册，type：1 微信注册
        Object result = "";
        if (type == 0) {
            if (StringUtils.isNoneBlank(phone)) {
                result = createUser(phone, nickname, gender, aboutMe, wxuid, icon);
            }
        } else if (type == 1) {
            if (StringUtils.isNoneBlank(wxuid)) {
                result = createUser(phone, nickname, gender, aboutMe, wxuid, icon);
            }
        }

        return result;

    }

    @Transactional(readOnly = false)
    public Object createUser(String phone, String nickname, Integer gender, String aboutMe, String wxuid, String icon) {
        UserRecordEntity userEntity = new UserRecordEntity();
        userEntity.setPhone(phone);
        userEntity.setNickname(nickname);
        userEntity.setGender(gender);
        userEntity.setAboutMe(aboutMe);
        userEntity.setWxuid(wxuid);
        userEntity.setIcon(icon);
        userRecordMapper.createUser(userEntity);
        Integer id = userEntity.getId();
        if (null != id) {
            UserRecordEntity userRecordEntity = userRecordMapper.selectById(id);
            return userRecordEntity;
        }
        return "";

    }

    @Override
    public Object getUserRecordState(String phone, String wxuid) {
        if (StringUtils.isBlank(phone)) {
            phone = "";
        }
        if (StringUtils.isBlank(wxuid)) {
            wxuid = "";
        }
        List<UserRecordEntity> userList = userRecordMapper.getUserListbyPhoneOrWxuid(phone, wxuid);
        if (userList != null && userList.size() > 0) {

            return userList.get(0);
        }

        return "create";
    }

    @Override
    public Object UpdateUserRecordByType(Integer uid, String phone, String nickname, Integer gender, String aboutMe, String wxuid, String icon) {
        UserRecordEntity userRecordEntity = userRecordMapper.selectById(uid);
        userRecordEntity.setPhone(phone);
        userRecordEntity.setNickname(nickname);
        userRecordEntity.setGender(gender);
        userRecordEntity.setAboutMe(aboutMe);
        userRecordEntity.setWxuid(wxuid);
        userRecordEntity.setIcon(icon);


        List<UserRecordEntity> userList = userRecordMapper.getUserListbyPhoneOrWxuid(userRecordEntity.getPhone(), userRecordEntity.getWxuid());
        if (userList != null && userList.size() > 1) {

            return "";
        }
        Integer id = userRecordMapper.updateById(userRecordEntity);
        return userRecordEntity;
    }

    @Override
    public Object getUserRecordInfo(Integer uid) {
        UserRecordEntity userRecordEntity = userRecordMapper.selectById(uid);
        if (null != userRecordEntity) {

            return userRecordEntity;
        }
        return "";

    }
}